//
//  AFNByLN.m
//  TTSAmericanMovie
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AFNByLN.h"
#import <AFNetworking.h>
@implementation AFNByLN
#pragma - mark - 计算多久前
+(NSString *) compareCurrentTime:(NSDate*) compareDate
{   NSTimeInterval  timeIntervall = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval timeInter = [compareDate timeIntervalSince1970];
    NSTimeInterval timeInterval = -( timeInter - timeIntervall);
    
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}
#pragma mark - 适配
+ (CGRect)fitIphone6WithX:(CGFloat)X
                    withY:(CGFloat)Y
                withWidth:(CGFloat)width
               withHeight:(CGFloat)height{
    return CGRectMake((CGFloat)X/375*WIDTH, (CGFloat)Y/667*HEIGHT, (CGFloat)width/375*WIDTH, (CGFloat)height/667*HEIGHT);
}
#pragma mark - 将秒数转换成时分秒的时间格式
+(NSString*)TimeformatFromSeconds:(NSInteger)seconds
{
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",seconds%3600/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_timeSmall = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    if (!(seconds/3600)>0) {
        return format_timeSmall;
    }else{
        return format_time;}
}
#pragma mark - 网络请求数据与缓存
+(void)GetDataWithUrlStr:(NSString *)url withSuccessBlock:(sucessBlock)sucessBlock withErrorBlock:(errorBlock)errorBlock{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        sucessBlock(responseObject);
        
        NSString *path = [NSString stringWithFormat:@"%ld*1000.plist", [url hash]];
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        if (result!= nil) {
             sucessBlock(result);
        }else{
            
            // 归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];}
        sucessBlock(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        errorBlock(error);
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld*1000.plist", [url hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        sucessBlock(result);
    }];
}
/**
 *  网络判断
    1#import "Reachability.h"
    2粘贴- (void)creatwang {}这个方法
 - (void)creatwang {
 Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
 NSString *string = [[NSString alloc] init];
 if ([r currentReachabilityStatus] == NotReachable) {
 string = @"当前没有网络连接, 请检查网络后重试";
 // 没有网络连接
 } else if ([r currentReachabilityStatus] == ReachableViaWiFi) {
 // 使用WiFi网络
 string = @"当前使用WiFi网络";
 } else {
 // 使用手机网络
 string = @"正在使用手机流量, 建议使用WiFi网络";
 }
 
 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
 /*添加action*/
/*UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
[alertController addAction:action1];

[self.parentViewController presentViewController:alertController animated:YES completion:^{}];
}
  3调用方法[self createwang]


 */
@end
